﻿using System;
using System.Collections.Generic;
using System.Text;
using AdaptiveAgents.Tools;
using System.Data;

namespace AdaptiveAgents.Formulas
{
    class AverageGradePerRound
    {
        private DataTable m_dtOutputTable;
        private List<AverageGradeRecord> m_dbDataBase;

        public AverageGradePerRound()
        {
            m_dtOutputTable = new DataTable();
            m_dbDataBase = new List<AverageGradeRecord>();
        }

        private void WriteToCSV(String fileName, DataTable dt)
        {
            //set an output writer
            System.IO.TextWriter writer = new System.IO.StreamWriter(fileName);

            //print columns name
            for (int i = 0; i < dt.Columns.Count; ++i)
                writer.Write(dt.Columns[i].ToString() + ",");

            writer.WriteLine();

            //for each row...
            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                DataRow row = dt.Rows[i];
                //for each cell in the row...
                for (int j = 0; j < dt.Columns.Count; ++j)
                    writer.Write(row[j].ToString() + ",");
                writer.WriteLine();
            }

            writer.Close();
        }

        public void addRecord(AverageGradeRecord rec)
        {
            m_dbDataBase.Add(rec);
        }

        public double solveRecord(AverageGradeRecord averageRec)
        {
            Record rec = averageRec.getRecord();
            ProjectCheckingTool tool = new ProjectCheckingTool();
            double[] probablities = tool.solveRecord(rec);
            double returnAnswer = probablities[0] * rec.getCompetence(0) + probablities[1] * rec.getCompetence(1) + probablities[2] * rec.getCompetence(2);

            return returnAnswer;
        }
        
        private double[] solveExstraRecord(AverageGradeRecord averageRec)
        {
            Record rec = averageRec.getRecord();
            ProjectCheckingTool tool = new ProjectCheckingTool();
            double[] probablities = tool.solveRecord(rec);
            double caculateAverageGrade = probablities[0] * rec.getCompetence(0) + probablities[1] * rec.getCompetence(1) + probablities[2] * rec.getCompetence(2);
            double[] returnAnswer = new double[4];

            for (int i = 0; i < 3;++i )
            {
                returnAnswer[i] = probablities[i];
            }
            returnAnswer[3] = caculateAverageGrade;

            return returnAnswer;
        }

        public void solveDataBase()
        {

            for (int i = 0; i < m_dbDataBase.Count; ++i)
            {
                double[] temp = solveExstraRecord(m_dbDataBase[i]);
                m_dbDataBase[i].setCalculateAverage(temp[3]);
                m_dbDataBase[i].setProbabilities(temp[0], 0);
                m_dbDataBase[i].setProbabilities(temp[1], 1);
                m_dbDataBase[i].setProbabilities(temp[2], 2);
            }
        }

        public void printResultToCSVFile()
        {
            DataTable printOut = new DataTable("Result_Of_Average_Grade_Per_Round_Tool");
            DataRow row;
            double[] tempArray;

            DataColumn cln = new DataColumn("Game_Number", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Competence2", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Competence1", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Competence0", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Epsilon2", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Epsilon1", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Epsilon0", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Percantage_Of_Action_Agent_2", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Percantage_Of_Action_Agent_1", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Percantage_Of_Action_Agent_0", typeof(double));
            printOut.Columns.Add(cln);

            cln = new DataColumn("Calaulate_Probability_Of_Agent_2", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Calaulate_Probability_Of_Agent_1", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("Calaulate_Probability_Of_Agent_0", typeof(double));
            printOut.Columns.Add(cln);

            cln = new DataColumn("AverageGrade", typeof(double));
            printOut.Columns.Add(cln);
            cln = new DataColumn("CalculateGradePerRound", typeof(double));
            printOut.Columns.Add(cln);
           

            for (int i = 0; i < m_dbDataBase.Count; ++i)
            {
                double ag0 = m_dbDataBase[i].getNumOfTurnsPlayed(0);
                double ag1 = m_dbDataBase[i].getNumOfTurnsPlayed(1);
                double ag2 = m_dbDataBase[i].getNumOfTurnsPlayed(2);
                double sum = ag0 + ag1 + ag2;

                row = printOut.NewRow();


                row["AverageGrade"] = m_dbDataBase[i].getAverage();
                row["CalculateGradePerRound"] = m_dbDataBase[i].getCalculateAverage();

                row["Percantage_Of_Action_Agent_0"] = (ag0 / sum);
                row["Percantage_Of_Action_Agent_1"] = (ag1 / sum);
                row["Percantage_Of_Action_Agent_2"] = (ag2 / sum);

                row["Calaulate_Probability_Of_Agent_0"] = m_dbDataBase[i].getProbabilities(0);
                row["Calaulate_Probability_Of_Agent_1"] = m_dbDataBase[i].getProbabilities(1);
                row["Calaulate_Probability_Of_Agent_2"] = m_dbDataBase[i].getProbabilities(2);

                row["Epsilon0"] = m_dbDataBase[i].getEpsilon(0);
                row["Epsilon1"] = m_dbDataBase[i].getEpsilon(1);
                row["Epsilon2"] = m_dbDataBase[i].getEpsilon(2);

                row["Competence0"] = m_dbDataBase[i].getCompetence(0);
                row["Competence1"] = m_dbDataBase[i].getCompetence(1);
                row["Competence2"] = m_dbDataBase[i].getCompetence(2);

                row["Game_Number"] = m_dbDataBase[i].getIndex();

                printOut.Rows.Add(row);
            }
            String stamp = DateTime.Now.ToString("yyyy.MM.dd-HH.mm.ss");
            WriteToCSV("Result_Of_Average_Grade_Per_Round_Tool_" + stamp + ".csv", printOut);
        }
      


    }
}
